package com.xr.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Arrays;

@Aspect
@Component("logAspect")
public class LogAspect {

  private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

  private static final String BEFORE_PATTERN = "start call {0} method, and input arguments is : {1}";
  private static final String AFTER_RETURN_PATTERN = "after call {0} method, and return value is : {1}";

  @Before("execution(* xr.service.impl.*.*(..))")
  public void beforMethod(JoinPoint jp) {
    logger.info(MessageFormat.format(BEFORE_PATTERN, jp.getSignature().getName(), Arrays.toString(jp.getArgs())));
  }

  @AfterReturning(value = "execution(* xr.service.impl.*.*(..))", argNames = "rtv", returning = "rtv")
  public void aferMethod(JoinPoint jp, Object rtv) {
    logger.info(MessageFormat.format(AFTER_RETURN_PATTERN, jp.getSignature().getName(), rtv));
  }

}
